//
//  ZLAlert.m
//  ZLAlert-OC
//
//  Created by 科大 on 2024/1/2.
//

#import "ZLAlert.h"
#import "ZLExtension+Extension.h"

@interface ZLAlert ()<UIViewControllerTransitioningDelegate>
@property (nonatomic, copy) ZlAlertAction closeAction;
@end

@implementation ZLAlert

- (instancetype)init {
    self = [super initWithNibName:nil bundle:nil];
    if (self) {
        self.modalPresentationStyle = UIModalPresentationOverFullScreen;
        self.transitioningDelegate = self;
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.view.backgroundColor = [UIColor colorWithWhite:0 alpha:0.1];
    
    [self.view addSubview:self.mainView];
    [self.mainView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.view).offset(35);
        make.right.equalTo(self.view).offset(-35);
        make.centerY.equalTo(self.view);
    }];
    
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(keyboardWillShow:)
                                                 name:UIKeyboardWillShowNotification
                                               object:nil];
    
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(keyboardWillHide:)
                                                 name:UIKeyboardWillHideNotification
                                               object:nil];
    
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(keyboardDidHide:)
                                                 name:UIKeyboardDidHideNotification
                                               object:nil];
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    if (ZLAlertConfig.shared.backgroundEnable) {
        [super touchesBegan:touches withEvent:event];
    }
}

- (ZLAlert *)addCustomView:(UIView *)customView {
    return [self addCustomView:customView insets:UIEdgeInsetsZero];
}

- (ZLAlert *)addCustomView:(UIView *)customView insets:(UIEdgeInsets)insets {
    if (!customView) {
        return [self layoutMainView:10];
    }
    UIView *lastView = self.mainView.subviews.lastObject;
    [self.mainView addSubview:customView];
    [customView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.mainView).offset(insets.left);
        make.right.equalTo(self.mainView).offset(-insets.right);
        if (lastView == nil) {
            make.top.equalTo(self.mainView).offset(insets.top);
        } else {
            make.top.equalTo(lastView.mas_bottom).offset(insets.top);
        }
    }];
    return [self layoutMainView:10];
}

- (void)show {
    UIViewController *topVC = [UIViewController zl_topViewController:nil];
    [topVC presentViewController:self animated:YES completion:nil];
}

- (void)close:(ZlAlertAction)closeAction {
    [self dismissViewControllerAnimated:YES completion:^{
        if (closeAction) {
            closeAction();
        }
    }];
}

- (void)showWithTapBackgroundClose:(ZlAlertAction)closeAction {
    [self show];
    self.closeAction = closeAction;
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapBackground)];
    [self.view addGestureRecognizer:tap];
}

- (void)tapBackground {
    [self close:self.closeAction];
}

- (ZLAlert *)layoutMainView:(CGFloat)bottom {
    UIView *lastView = self.mainView.subviews.lastObject;
    
    if (!lastView) {
        return self;
    }
    
    [self.mainView mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(lastView.mas_bottom).offset(bottom);
    }];
    
    return self;
}

#pragma mark - Keyboard Notifications

- (void)keyboardWillShow:(NSNotification *)notification {
    // 实现键盘即将显示的逻辑
}

- (void)keyboardWillHide:(NSNotification *)notification {
    // 实现键盘即将隐藏的逻辑
}

- (void)keyboardDidHide:(NSNotification *)notification {
    // 实现键盘已经隐藏的逻辑
}

#pragma mark - UIViewControllerTransitioningDelegate

- (id<UIViewControllerAnimatedTransitioning>)animationControllerForPresentedController:(UIViewController *)presented
                                                                  presentingController:(UIViewController *)presenting
                                                                      sourceController:(UIViewController *)source {
    return [[ZLAlertAnimation alloc] initWith:YES];
}

- (id<UIViewControllerAnimatedTransitioning>)animationControllerForDismissedController:(UIViewController *)dismissed {
    return [[ZLAlertAnimation alloc] initWith:NO];
}

- (ZLAlertMainView *)mainView {
    if (!_mainView) {
        _mainView = [[ZLAlertMainView alloc] init];
        _mainView.backgroundColor = [UIColor whiteColor];
        _mainView.layer.masksToBounds = YES;
        _mainView.layer.cornerRadius = 5.0;
    }
    return _mainView;
}

@end
